<?php
namespace app\admin\controller;
/**
 * #订单管理#{订单管理}
 */
class Order extends Common{
    /**
     * 虚拟币微豆订单
     * @return [type] [description]
     */
    function coin($range=''){
        if(is_string($range)){
            $range=explode(',', $range);
        }
        $params=[
            'range'=>$range,
            'keyword'=>input('keyword/s',''),
            'cate'=>input('cate/d',0),
            'status'=>input('status/d',0),
            'is_admin'=>input('is_admin/d',1),
            'pay'=>input('pay/s',''),
            'order'=>input('order/d',0),
        ];
        $map=['type'=>2];

        $options=[
            'with'=>['user','parent','vip'],
            'order'=>'s_time desc'
        ];

        if(!empty($params['keyword'])){
            if($params['cate']){ //通过邀请人电话号码查询
                $parent=model('member')->read(['tel'=>$params['keyword']],'id');
                $map['pid']=$parent['id'];
            }else{ //通过下单人电话号码、订单号查询
                $map['tel|number']=['tel|number','like',"%{$params['keyword']}%"];
            }
        }
        if($params['status']>0){
            $map['state']=$params['status']-1;
        }
        if($params['is_admin']>0){
            $map['is_admin']=$params['is_admin']-1;
        }
        if($params['pay'] != ''){
            if ($params['pay'] == 'null') {
                $map['pay_type']='';
            }
            else{
                $map['pay_type']=$params['pay'];
            }
        }
        if($params['order']>0){

            if ($params['order'] == 1) {
                $options['order'] = 'e_time desc';
            }
            else{
                $params['order'] = '';
                // $options['order'] = 'e_time asc';
            }
        }
        if(!empty($range)){
            if(count($range)===2){
                $map['s_time']=['s_time','between',[$range[0]/1000,$range[1]/1000+86400]];
            }else{
                $map['s_time']=['s_time','>=',$range[0]/1000];
            }
        }
        $list=model('order')->readAll($map,true,$options)->appends($params);
        $this->assign('list',$list);

        $this->assign('params',json_encode($params));
        

        return view();
    }
    /**
     * VIP订单
     * @return [type] [description]
     */
    function vip($range=''){
        if(is_string($range)){
            $range=explode(',', $range);
        }
        $params=[
            'range'=>$range,
            'keyword'=>input('keyword/s',''),
            'cate'=>input('cate/d',0),
            'vip_id'=>input('vip_id/d',0),
            'status'=>input('status/d',0),
            'is_admin'=>input('is_admin/d',1),
            'pay'=>input('pay/s',''),
            'order'=>input('order/d',0),
        ];
        $map=['type'=>1];

        $options=[
            'with'=>['user','parent','vip'],
            'order'=>'s_time desc'
        ];

        if(!empty($params['keyword'])){
            if($params['cate']){ //通过邀请人电话号码查询
                $parent=model('member')->read(['tel'=>$params['keyword']],'id');
                $map['pid']=$parent['id'];
            }else{ //通过下单人电话号码、订单号查询
                $map['tel|number']=['tel|number','like',"%{$params['keyword']}%"];
            }
        }
        if($params['vip_id']>0){
            $map['vid']=$params['vip_id'];
        }
        if($params['status']>0){
            $map['state']=$params['status']-1;
        }
        if($params['is_admin']>0){
            $map['is_admin']=$params['is_admin']-1;
        }
        if($params['pay'] != ''){
            if ($params['pay'] == 'null') {
                $map['pay_type']='';
            }
            else{
                $map['pay_type']=$params['pay'];
            }
        }
        if($params['order']>0){

            if ($params['order'] == 1) {
                $options['order'] = 'e_time desc';
            }
            else{
                $params['order'] = '';
                // $options['order'] = 'e_time asc';
            }
        }
        if(!empty($range)){
            if(count($range)===2){
                $map['s_time']=['s_time','between',[$range[0]/1000,$range[1]/1000+86400]];
            }else{
                $map['s_time']=['s_time','>=',$range[0]/1000];
            }
        }
        $list=model('order')->readAll($map,true,$options)->appends($params);
        $this->assign('list',$list);

        $vip=model('vip')->readAll(['status'=>1],0,['field'=>'id,name,level','order'=>'sort'])->toArray();
        array_unshift($vip, ['id'=>0,'name'=>'全部级别']);
        $this->assign('vip',json_encode($vip));
        $this->assign('params',json_encode($params));
        
        return view();
    }

    function info($id=0){
        if(request()->isAjax()||request()->isPost()){
            $res=model('order')->read(['id'=>$id],'*',['with'=>['user','vip','coin']]);
            if($res['pay_type']==='alipay'){
                $res['realname']=$res['alipay_realname'];
            }elseif($res['pay_type']==='weixinpay'){
                $res['realname']=$res['weixin_realname'];
            }else{
                $res['realname']=empty($res['alipay_realname'])?$res['weixin_realname']:$res['alipay_realname'];
            }
            
            if($res['type']==1){
                $res['product_name']=$res['vip'];
            }elseif($res['type']==2){
                $res['product_name']=lang('coin').$res['coin'];
            }else{
                $res['product_name']='--';
            }
            $this->success('','',$res);
        }
    }
}